package com.dd.cloud.user.service.chknowl.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.user.entity.chknowl.ChkonwlDoctorBookReadRecord;
import com.dd.cloud.user.mapper.chknowl.ChkonwlDoctorBookReadRecordMapper;
import com.dd.cloud.user.service.chknowl.IChkonwlDoctorBookReadRecordService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 医生书籍阅读记录 服务实现类
 * </p>
 *
 * @author czg
 * @since 2023-10-17
 */
@Service
public class ChkonwlDoctorBookReadRecordServiceImpl extends ServiceImpl<ChkonwlDoctorBookReadRecordMapper, ChkonwlDoctorBookReadRecord> implements IChkonwlDoctorBookReadRecordService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addDoctorBookReadFirstRecord(Integer doctorId) {
        Boolean flag = this.queryDoctorBookReadFirstRecord(doctorId);
        if (!flag) {
            ChkonwlDoctorBookReadRecord readRecord = new ChkonwlDoctorBookReadRecord();
            readRecord.setDoctorId(doctorId)
                    .setCreateDate(LocalDateTime.now());
            Boolean flag1 = this.save(readRecord);
            if (!flag1) {
                throw new ZekeException("保存数据记录失败");
            }
        }
    }

    @Override
    public Boolean queryDoctorBookReadFirstRecord(Integer doctorId) {
        LambdaQueryWrapper<ChkonwlDoctorBookReadRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChkonwlDoctorBookReadRecord::getDoctorId, doctorId);
        Integer count = this.count(wrapper);
        if (count > 0) {
            return true;
        }
        return false;
    }
}
